package com.lottery.ruok.lottrey.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.FilterOrderAdapter;
import com.lottery.ruok.lottrey.eventbusObject.OrderFilter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetWholeCategoryRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetWholeCategoryResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.NoScrollGridView;

/*
* 筛选
* */

public class FilterOrderActivity extends BaseActivity {

    @BindView(R.id.types)
    NoScrollGridView types;
    @BindView(R.id.today)
    TextView today;
    @BindView(R.id.yesterday)
    TextView yesterday;
    @BindView(R.id.this_week)
    TextView thisWeek;
    @BindView(R.id.last_week)
    TextView lastWeek;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_time)
    TextView endTime;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.close)
    ImageView close;

    public String mCurrentName = "全部";
    private String mCode = null;
    private String mStartTime = null;
    private String mEndTime = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.filter_order_dialig);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        RetrofitBase.getRetrofit().create(GetWholeCategoryRequest.class).getResult(UserInfo.getToken(), 0, 0)
                .compose(RxHelper.<ArrayList<GetWholeCategoryResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetWholeCategoryResponse>>(this) {
                    @Override
                    protected void _onNext(final ArrayList<GetWholeCategoryResponse> getWholeCategoryResponses) {
                        int index = -1;
                        for(int i = 0; i < getWholeCategoryResponses.size(); i++){
                            if(getWholeCategoryResponses.get(i).getName().equals("全部")){
                                index = i;
                                break;
                            }
                        }
                        if(index != -1) {
                            final int postion = index;
                            final FilterOrderAdapter adapter = new FilterOrderAdapter(getWholeCategoryResponses, index, FilterOrderActivity.this);
                            types.setAdapter(adapter);
                            types.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    mCurrentName = getWholeCategoryResponses.get(postion).getItems().get(i).getName();
                                    mCode = getWholeCategoryResponses.get(postion).getItems().get(i).getCode();
                                    adapter.notifyDataSetChanged();
                                }
                            });
                        }
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }

    @Override
    public void finish() {
        super.finish();

        overridePendingTransition(0, R.anim.anim_exit_from_bottom);
    }

    @OnClick({R.id.today, R.id.yesterday, R.id.this_week, R.id.last_week, R.id.start_time, R.id.end_time, R.id.confirm, R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.today:
                today.setBackgroundResource(R.drawable.bg_corners_100_3);
                yesterday.setBackgroundResource(R.drawable.bg_corners_100_12);
                thisWeek.setBackgroundResource(R.drawable.bg_corners_100_12);
                lastWeek.setBackgroundResource(R.drawable.bg_corners_100_12);

                today.setTextColor(Color.parseColor("#ffffff"));
                yesterday.setTextColor(Color.parseColor("#3f3f3f"));
                thisWeek.setTextColor(Color.parseColor("#3f3f3f"));
                lastWeek.setTextColor(Color.parseColor("#3f3f3f"));

                startTime.setText("");
                endTime.setText("");

                startTime.setBackgroundResource(R.drawable.bg_corners_24);
                endTime.setBackgroundResource(R.drawable.bg_corners_24);

                Date d = new Date(System.currentTimeMillis());
                SimpleDateFormat sp= new SimpleDateFormat("yyyy-MM-dd");
                String ZUOTIAN = sp.format(d);
                mStartTime = ZUOTIAN + " 00:00:00";
                mEndTime = ZUOTIAN + " 23:59:59";
                break;
            case R.id.yesterday:
                today.setBackgroundResource(R.drawable.bg_corners_100_12);
                yesterday.setBackgroundResource(R.drawable.bg_corners_100_3);
                thisWeek.setBackgroundResource(R.drawable.bg_corners_100_12);
                lastWeek.setBackgroundResource(R.drawable.bg_corners_100_12);

                today.setTextColor(Color.parseColor("#3f3f3f"));
                yesterday.setTextColor(Color.parseColor("#ffffff"));
                thisWeek.setTextColor(Color.parseColor("#3f3f3f"));
                lastWeek.setTextColor(Color.parseColor("#3f3f3f"));

                startTime.setText("");
                endTime.setText("");

                startTime.setBackgroundResource(R.drawable.bg_corners_24);
                endTime.setBackgroundResource(R.drawable.bg_corners_24);

                Date d1 = new Date(System.currentTimeMillis()-1000*60*60*24);
                SimpleDateFormat sp1= new SimpleDateFormat("yyyy-MM-dd");
                String ZUOTIAN1 = sp1.format(d1);
                mStartTime = ZUOTIAN1 + " 00:00:00";
                mEndTime = ZUOTIAN1 + " 23:59:59";
                break;
            case R.id.this_week:
                today.setBackgroundResource(R.drawable.bg_corners_100_12);
                yesterday.setBackgroundResource(R.drawable.bg_corners_100_12);
                thisWeek.setBackgroundResource(R.drawable.bg_corners_100_3);
                lastWeek.setBackgroundResource(R.drawable.bg_corners_100_12);

                today.setTextColor(Color.parseColor("#3f3f3f"));
                yesterday.setTextColor(Color.parseColor("#3f3f3f"));
                thisWeek.setTextColor(Color.parseColor("#ffffff"));
                lastWeek.setTextColor(Color.parseColor("#3f3f3f"));

                startTime.setText("");
                endTime.setText("");

                startTime.setBackgroundResource(R.drawable.bg_corners_24);
                endTime.setBackgroundResource(R.drawable.bg_corners_24);

                Calendar cal = Calendar.getInstance();
                Calendar first = (Calendar) cal.clone();
                first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK) + 1);
                Calendar last = (Calendar) first.clone();
                last.add(Calendar.DAY_OF_YEAR, 6);
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                mStartTime = df.format(first.getTime());
                mEndTime = df.format(last.getTime());
                break;
            case R.id.last_week:
                today.setBackgroundResource(R.drawable.bg_corners_100_12);
                yesterday.setBackgroundResource(R.drawable.bg_corners_100_12);
                thisWeek.setBackgroundResource(R.drawable.bg_corners_100_12);
                lastWeek.setBackgroundResource(R.drawable.bg_corners_100_3);

                today.setTextColor(Color.parseColor("#3f3f3f"));
                yesterday.setTextColor(Color.parseColor("#3f3f3f"));
                thisWeek.setTextColor(Color.parseColor("#3f3f3f"));
                lastWeek.setTextColor(Color.parseColor("#ffffff"));

                startTime.setText("");
                endTime.setText("");

                startTime.setBackgroundResource(R.drawable.bg_corners_24);
                endTime.setBackgroundResource(R.drawable.bg_corners_24);

                Calendar cal1 = Calendar.getInstance();
                Calendar first1 = (Calendar) cal1.clone();
                first1.add(Calendar.DAY_OF_WEEK, first1.getFirstDayOfWeek() - first1.get(Calendar.DAY_OF_WEEK) - 6);
                Calendar last1 = (Calendar) first1.clone();
                last1.add(Calendar.DAY_OF_YEAR, 6);
                SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
                mStartTime = df1.format(first1.getTime());
                mEndTime = df1.format(last1.getTime());
                break;
            case R.id.start_time:
                Intent intent = new Intent(FilterOrderActivity.this, ChooseDateActivity.class);
                if(!endTime.getText().toString().equals("")){
                    intent.putExtra("endTime", endTime.getText().toString() + " 00:00:00");
                }
                if(!startTime.getText().toString().equals("")){
                    intent.putExtra("time", startTime.getText().toString() + " 00:00:00");
                }
                startActivityForResult(intent, 101);
                overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                break;
            case R.id.end_time:
                Intent intent1 = new Intent(FilterOrderActivity.this, ChooseDateActivity.class);
                if(!startTime.getText().toString().equals("")){
                    intent1.putExtra("startTime", startTime.getText().toString() + " 00:00:00");
                }
                if(!endTime.getText().toString().equals("")){
                    intent1.putExtra("time", endTime.getText().toString() + " 00:00:00");
                }
                startActivityForResult(intent1, 102);
                overridePendingTransition(R.anim.anim_enter_from_bottom, R.anim.activity_stay);
                break;
            case R.id.confirm:
                if(mStartTime != null && mEndTime != null){
                    OrderFilter orderFilter = new OrderFilter();
                    orderFilter.setCode(mCode);
                    orderFilter.setStart(mStartTime);
                    orderFilter.setEnd(mEndTime);
                    EventBus.getDefault().post(orderFilter);
                } else if(startTime.getText().length() > 0 && endTime.getText().length() > 0){
                    OrderFilter orderFilter = new OrderFilter();
                    orderFilter.setCode(mCode);
                    orderFilter.setStart(startTime.getText().toString());
                    orderFilter.setEnd(endTime.getText().toString());
                    EventBus.getDefault().post(orderFilter);
                } else {
                    OrderFilter orderFilter = new OrderFilter();
                    orderFilter.setCode(mCode);
                    orderFilter.setStart(null);
                    orderFilter.setEnd(null);
                    EventBus.getDefault().post(orderFilter);
                }
                finish();
                break;
            case R.id.close:
                finish();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
            startTime.setText(data.getStringExtra("date").split(" ")[0]);
            startTime.setBackgroundResource(R.drawable.bg_corners_14);

            today.setTextColor(Color.parseColor("#3f3f3f"));
            yesterday.setTextColor(Color.parseColor("#3f3f3f"));
            thisWeek.setTextColor(Color.parseColor("#3f3f3f"));
            lastWeek.setTextColor(Color.parseColor("#3f3f3f"));

            today.setBackgroundResource(R.drawable.bg_corners_100_12);
            yesterday.setBackgroundResource(R.drawable.bg_corners_100_12);
            thisWeek.setBackgroundResource(R.drawable.bg_corners_100_12);
            lastWeek.setBackgroundResource(R.drawable.bg_corners_100_12);

            mStartTime = null;
            mEndTime = null;
        } else if (requestCode == 102 && resultCode == Activity.RESULT_OK) {
            endTime.setText(data.getStringExtra("date").split(" ")[0]);
            endTime.setBackgroundResource(R.drawable.bg_corners_14);

            today.setTextColor(Color.parseColor("#3f3f3f"));
            yesterday.setTextColor(Color.parseColor("#3f3f3f"));
            thisWeek.setTextColor(Color.parseColor("#3f3f3f"));
            lastWeek.setTextColor(Color.parseColor("#3f3f3f"));

            today.setBackgroundResource(R.drawable.bg_corners_100_12);
            yesterday.setBackgroundResource(R.drawable.bg_corners_100_12);
            thisWeek.setBackgroundResource(R.drawable.bg_corners_100_12);
            lastWeek.setBackgroundResource(R.drawable.bg_corners_100_12);

            mStartTime = null;
            mEndTime = null;
        }
    }
}
